package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@author liwei
*@date 2021/4/6
*description:
*/
public class PackersEngineeringTenderModel extends AndroidViewModel {

    private PickSelectEntity all;

    private int page = 1;
    private int limit = 10;
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<TenderListEntity>> subPackageList = new MutableLiveData<>();
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> popList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> tenderMethodList = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> cityList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>> buildTypeList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> timeList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> priceList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> tenderTypeList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> provinceList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> cityDataList = new MutableLiveData<>();
    public MutableLiveData<String> urlStr = new MutableLiveData<>();

    public PackersEngineeringTenderModel(Application application) {
        super(application);
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getNewList(String goodsType, String priceNo, String time, String verticalType, String areaCode, String cityNo, boolean b) {
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("limit", limit);
        map.put("page", page);
        if (!"".equals(areaCode) && areaCode != null) {
            map.put("areacode", areaCode);
        }
        if (!"".equals(cityNo) && cityNo != null) {
            map.put("city", cityNo);
        }
        map.put("comName", "");
        if (!"".equals(time) && time != null) {
            // 报名结束时间
            map.put("time", time);
        }
        if (!"".equals(verticalType) && verticalType != null) {
            // 报名方式：1、资格预审；2、资格后审；3、报价即报名
            map.put("verticalType", verticalType);
        }
        if (!"".equals(priceNo) && priceNo != null) {
            map.put("price", priceNo);
        }
        if (!TextUtils.isEmpty(goodsType)) {
            // 清单分类类型
            map.put("goodsType", goodsType);
        }
        // 招标类型
        map.put("type", "4");
        map.put("sortMethod", "1");
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTenderApplyingList(map).enqueue(new ICallBack<BaseEntity<List<TenderListEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<TenderListEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() != null) {
                    subPackageList.postValue(data.getData());
                }
            }
        });
    }


    public void getTenderProjectList() {
        loading.postValue(true);
        Map<String,Object> map = new HashMap<>();
        map.put("type", "2");
        APIManager.getInstance().getPackerAPI().getTenderProjectList(map).enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                popList.postValue(data.getData());
            }
        });
    }


    public void getTenderMethodList() {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getTenderNewTypeList().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                tenderMethodList.postValue(data.getData());
            }
        });
    }


    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                cityList.postValue(data.getData());
            }
        });
    }


    public void getTenderGoodsCategoryAllList() {
        loading.postValue(true);
        Map<String,Object> map = new HashMap<>();
        map.put("type", "2");
        APIManager.getInstance().getPickAPI().getTenderGoodsCategoryAllList(map).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                buildTypeList.postValue(data.getData());
            }
        });
    }

    public void getPickTimes() {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getPickTimes().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                timeList.postValue(data.getData());
            }
        });
    }


    public void getPriceData() {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                priceList.postValue(data.getData());
            }
        });
    }


    public void getTenderNewTypeList() {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getTenderNewTypeList().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                tenderTypeList.postValue(data.getData());
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getProvinceData(String areaCode) {
        Map<String,Object> map = new HashMap<>();
        map.put("areaCode", areaCode);
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getLocationNew(map).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                provinceList.postValue(data.getData());
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData(String province) {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getAreaSecond(province).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                List<PickSelectEntity> data1 = data.getData();
                data1.add(0, all);
                cityDataList.postValue(data1);
            }
        });
    }

    public void getUrlSettingTender(String id) {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getUrlSetting("FrontDeskBidding").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                String url = data.getData().getUrl() + "?id=" + id;
                String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                url = url + "&lng=" + lng + "&lat=" + lat;
                urlStr.postValue(url+"111"+id);
            }
        });
    }
}
